import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入对应组件
/*用户登录页面组件*/
import userLogin from '../components/User/userLogin.vue'
/*用户注册页面组件*/
import userRegister from '../components/User/userRegister.vue'
/*客户主页页面组件*/
import Home from '../components/Home.vue'
/*用户主页页面组件*/
import userHome from '../components/User/userHome.vue'
/*后台管理页面组件*/
import adminHome from '../components/Admin/adminHome.vue'
/*后台登录页面组件*/
import adminLogin from '../components/Admin/adminLogin.vue'
/*后台用户管理组件*/
import user from '../components/Admin/User/user.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/user/login' },
  { path: '/user/login', component: userLogin, },
  { path: '/user/register', component: userRegister, },
  { path: '/home', component: Home},
  { 
    path: '/user/home', 
    component: userHome,
    children: [
      
    ]
  },
  { path: '/manager/login', component: adminLogin},
  { 
    path: '/manager/home',
    component: adminHome, 
    children: [
      { path: '/manager/user', component: user },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if(to.path == '/user/login' || to.path == '/manager/login' || to.path == '/user/register') 
    return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) {
    let pathURL = to.path.split('/')[1]
    if (pathURL == 'user') {
      return next('/user/login')
    } else if (pathURL == 'manager') {
      return next('/manager/login')
    }
  }
  next()
})

export default router
